<template>
  <div class="statistics-page">
    <!-- 统计科技风背景 -->
    <div class="stats-background">
      <div class="heatmap-grid">
        <div v-for="i in 120" :key="i" class="heatmap-cell" :style="getHeatmapStyle(i)"></div>
      </div>
      <div class="chart-lines">
        <svg class="line-chart" viewBox="0 0 100 50">
          <polyline 
            v-for="i in 3" :key="i"
            :points="generateChartLine(i)"
            class="chart-line"
            :style="{ animationDelay: `${i * 0.5}s` }"
          />
        </svg>
      </div>
      <div class="data-points">
        <div v-for="i in 25" :key="i" class="data-point" :style="getDataPointStyle(i)"></div>
      </div>
    </div>

    <div class="ios-container">
      <div class="ios-navbar">
        <IOSBackButton @click="router.back()" />
      </div>
      <div class="content-wrapper">
        <h1 class="page-title">📈 Gitee热力图</h1>
        <p class="page-description">代码提交可视化，生成精美的贡献热力图，展示开发活动统计</p>
        
        <div class="action-section">
          <button class="action-btn primary" @click="toLoginGitee">
            🔐 码云授权登录
          </button>
          <button class="action-btn secondary" @click="toGetAccessToken">
            🔑 获取访问令牌
          </button>
        </div>

        <div class="feature-grid">
          <div class="feature-card">
            <div class="feature-icon">🔥</div>
            <h3>贡献热力图</h3>
            <p>可视化提交活动</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">📊</div>
            <h3>数据统计</h3>
            <p>详细活动分析</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">⏰</div>
            <h3>时间轴视图</h3>
            <p>按时间查看趋势</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import IOSBackButton from '~/components/IOSBackButton.vue'
import { getQuery } from 'h3'

const router = useRouter()
const route = useRoute()

const client_id = '9641feb7c6621091c08c9b0dcfe420f927d4f58de85700f960620886268d0417';
const client_secret = '10638d53bd0d1a6e9d624c39ad8f10d88fc28d738cd78ff030af8ba1234f1d48'
const redirect_uri = encodeURIComponent('https://192.168.2.102:3000/statistics');

const getHeatmapStyle = (index) => {
  const row = Math.floor((index - 1) / 20)
  const col = (index - 1) % 20
  const intensity = Math.random()
  return {
    gridRow: row + 1,
    gridColumn: col + 1,
    opacity: intensity,
    animationDelay: `${Math.random() * 3}s`
  }
}

const getDataPointStyle = (index) => {
  return {
    top: `${Math.random() * 80 + 10}%`,
    left: `${Math.random() * 80 + 10}%`,
    animationDelay: `${index * 0.1}s`
  }
}

const generateChartLine = (lineIndex) => {
  const points = []
  for (let i = 0; i <= 100; i += 10) {
    const y = 25 + Math.sin((i + lineIndex * 30) * 0.1) * 15 + Math.random() * 5
    points.push(`${i},${y}`)
  }
  return points.join(' ')
}

const toLoginGitee = () => {
  window.location.href = `https://gitee.com/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=code`
}

const toGetAccessToken = async () => {
  const query = getQuery(toRef(route, 'query'))
  const code = query.code;

  if (code) {
    console.log(code)
  }

  window.location.href = `https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${client_id}&redirect_uri=${redirect_uri}&client_secret=${client_secret}`
}

const user_cookie = useCookie('user', {
  default: () => ({ access_token: '' }),
  maxAge: 60 * 60 * 24 * 7,
  sameSite: 'lax',
})

const handleVisibilityChange = async () => {
  const query = getQuery(toRef(route, 'query'))
  const code = query.code || 'dd914011ed01033f702ec89f0ac99babbfc484a9b9fe772d35ef2ac3829e4805'

  if (code) {
    const res = await fetch(`https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${client_id}&redirect_uri=${redirect_uri}&client_secret=${client_secret}`)
    const data = await res.json()
    console.log(data)

    const access_token = data.access_token;
    user_cookie.value.access_token = access_token; 
  }
}

onActivated(handleVisibilityChange)
</script>

<style scoped>
.statistics-page {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.stats-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  z-index: -1;
}

.heatmap-grid {
  position: absolute;
  top: 10%;
  left: 10%;
  width: 80%;
  height: 30%;
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 2px;
}

.heatmap-cell {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  animation: heatmapPulse 2s ease-in-out infinite;
}

.chart-lines {
  position: absolute;
  bottom: 20%;
  left: 10%;
  width: 80%;
  height: 25%;
}

.line-chart {
  width: 100%;
  height: 100%;
  opacity: 0.6;
}

.chart-line {
  fill: none;
  stroke: rgba(255, 255, 255, 0.8);
  stroke-width: 1.5;
  stroke-linecap: round;
  animation: chartDraw 3s ease-in-out infinite;
}

.data-points {
  position: absolute;
  width: 100%;
  height: 100%;
}

.data-point {
  position: absolute;
  width: 6px;
  height: 6px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.6);
  animation: pointPulse 2s ease-in-out infinite;
}

.ios-container {
  position: relative;
  z-index: 1;
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.ios-navbar {
  padding: 10px 0;
  margin-bottom: 20px;
}

.content-wrapper {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  color: white;
  margin-bottom: 16px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  text-align: center;
}

.page-description {
  font-size: 1.2rem;
  color: rgba(255, 255, 255, 0.9);
  text-align: center;
  margin-bottom: 40px;
}

.action-section {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-bottom: 40px;
}

.action-btn {
  padding: 12px 24px;
  border-radius: 25px;
  border: none;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 1rem;
}

.action-btn.primary {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.action-btn.primary:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

.action-btn.secondary {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.action-btn.secondary:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 40px;
}

.feature-card {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 30px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.feature-card:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-5px);
}

.feature-icon {
  font-size: 2.5rem;
  margin-bottom: 15px;
}

.feature-card h3 {
  color: white;
  font-size: 1.3rem;
  margin-bottom: 10px;
  font-weight: 600;
}

.feature-card p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.95rem;
}

@keyframes heatmapPulse {
  0%, 100% { 
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1);
  }
  50% { 
    background: rgba(255, 255, 255, 0.4);
    transform: scale(1.1);
  }
}

@keyframes chartDraw {
  0% { 
    stroke-dasharray: 0 1000;
    opacity: 0.3;
  }
  50% { 
    stroke-dasharray: 500 500;
    opacity: 0.8;
  }
  100% { 
    stroke-dasharray: 1000 0;
    opacity: 0.3;
  }
}

@keyframes pointPulse {
  0%, 100% { 
    transform: scale(1);
    opacity: 0.6;
  }
  50% { 
    transform: scale(1.3);
    opacity: 1;
  }
}

@media (max-width: 768px) {
  .content-wrapper {
    padding: 20px;
  }
  
  .page-title {
    font-size: 2rem;
  }
  
  .action-section {
    flex-direction: column;
    align-items: center;
  }
  
  .feature-grid {
    grid-template-columns: 1fr;
  }
}
</style>
